unit RttiUtils;

interface

uses
  System.Rtti;

type

  TRttiUtils = class(TObject)
  public

    class procedure SetProperty(const iInstance:Pointer; const iClassType:TClass; const iPropertyName:string; const iValue:TValue);
    class procedure SetField(const iInstance:Pointer; const iClassType:TClass; const iFieldName:string; const iValue:TValue);

  end;

implementation

class procedure TRttiUtils.SetProperty(const iInstance:Pointer; const iClassType:TClass; const iPropertyName:string; const iValue:TValue);
var
  RttiContext:TRttiContext;
  RttiType:TRttiType;
  RttiProperty:TRttiProperty;
//  RttiMethod:TRttiMethod;
begin
  RttiContext.GetTypes;

  RttiType:=RttiContext.GetType(iClassType);
  if RttiType=nil then
  begin
    Exit;
  end;

  RttiProperty:=RttiType.GetProperty(iPropertyName);
  if RttiProperty=nil then
  begin
    Exit;
  end;

  RttiProperty.SetValue(iInstance,iValue);
end;

class procedure TRttiUtils.SetField(const iInstance:Pointer; const iClassType:TClass; const iFieldName:string; const iValue:TValue);
var
  RttiContext:TRttiContext;
  RttiType:TRttiType;
  RttiField:TRttiField;
begin
  RttiContext.GetTypes;

  RttiType:=RttiContext.GetType(iClassType);
  if RttiType=nil then
  begin
    Exit;
  end;

  RttiField:=RttiType.GetField(iFieldName);
  if RttiField=nil then
  begin
    Exit;
  end;

  RttiField.SetValue(iInstance,iValue);
end;

end.
